package com.ydc.ln.tutorial.httpclient.test;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

/**
 * HttpClient并发50个访问量
 * 
 * 
 * http://www.safedog.cn/
 * 
 * @author yangdc
 * 
 */
public class HttpClientDemo01 {

    public static void goto1(String url) throws Exception {

        DefaultHttpClient httpclient = new DefaultHttpClient(); // 实例化一个HttpClient
        // httpclient.getHostConfiguration().setProxy("10.130.40.13", 8026);

        // 设置代理
//         HttpHost proxy = new HttpHost("localhost", 8080);
//         httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
//         proxy);

        HttpResponse response = null;
        HttpEntity entity = null;
        httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // 设置cookie的兼容性

        HttpPost httpost = new HttpPost(url); // 引号中的参数是：servlet的地址

        // 参数
        // List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        // nvps.add(new BasicNameValuePair("list", "sh600872"));
        // nvps.add(new BasicNameValuePair("list", "sh600872"));
        // httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // 将参数传入post方法中

        response = httpclient.execute(httpost); // 执行
        entity = response.getEntity(); // 返回服务器响应
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine()); // 服务器返回状态
            Header[] headers = response.getAllHeaders(); // 返回的HTTP头信息
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");
            String responseString = null;
            // if (response.getEntity() != null) {
            // responseString = EntityUtils.toString(response.getEntity()); // 返回服务器响应的HTML代码
            // System.out.println(responseString); // 打印出服务器响应的HTML代码
            // }
        } finally {
            if (entity != null) entity.consumeContent(); // release connection gracefully
        }
        if ((response.getStatusLine().toString().contains("Unauthorized"))
                || response.getStatusLine().toString().contains("Forbidden")) {
            System.out.println("被识别了，将停止程序：" + response.getStatusLine());
            System.exit(1);
        }

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }

    }

    static long c = 0;

    public static void main(String[] args) throws Exception {
        try {

            for (int i = 0; i < 50; i++) {
                new Thread(new Runnable() {
                    public void run() {
                        // http://www.pu21pu.com/wjd/com/user.asp
                        // http://localhost:8080/exam/admin/score/reviewPaper.jsp
                        // http://192.168.1.116:8080/exam/
                        // http://www.yadian.cc/blog_news.asp
                        // http://www.itivy.com/snddfhv
                        // http://www.itivy.com/snddfhv/archive/2011/12/27/634605898888525812.html
                        // http://iljf.v084.10000net.cn/
                        // http://hnws233.com/
                        // http://ttcztv.com/
                        String[] xx = new String[] {"http://ttcztv.com/"};
                        while (true) {
                            for (int j = 0; j < xx.length; j++) {
                                try {
                                    goto1(xx[j]);
                                } catch (Exception e) {}
                                c++;
                                System.out.println("c------------------:" + c);
                            }
                        }
                    }
                }).start();
            }
        } catch (Exception e) {
            Thread.sleep(5000);
            e.printStackTrace();
            HttpClientDemo01.main(null);
        }
    }

    public static void xxx() throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient(); // 实例化一个HttpClient

        HttpResponse response = null;
        HttpEntity entity = null;
        httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY); // 设置cookie的兼容性

        HttpPost httpost = new HttpPost("http://hq.sinajs.cn/list=s_sh000001"); // 引号中的参数是：servlet的地址

        // 参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("list", "sh600872"));
        nvps.add(new BasicNameValuePair("list", "sh600872"));
        httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); // 将参数传入post方法中
        response = httpclient.execute(httpost); // 执行
        entity = response.getEntity(); // 返回服务器响应
        try {
            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine()); // 服务器返回状态
            Header[] headers = response.getAllHeaders(); // 返回的HTTP头信息
            for (int i = 0; i < headers.length; i++) {
                System.out.println(headers[i]);
            }
            System.out.println("----------------------------------------");
            String responseString = null;
            if (response.getEntity() != null) {
                responseString = EntityUtils.toString(response.getEntity()); // 返回服务器响应的HTML代码
                System.out.println(responseString); // 打印出服务器响应的HTML代码
            }
        } finally {
            if (entity != null) entity.consumeContent(); // release connection gracefully
        }
        // Login form get: HTTP/1.1 401 Unauthorized
        if ((response.getStatusLine().toString().contains("Unauthorized"))) {
            System.out.println(response.getStatusLine());
            System.exit(1);
        }
        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            entity.consumeContent();
        }
    }
}
